<template>
  <div class="commentReply">
    <div class="cmtBody">
      <div class="userSay">
        <span>{{parentData.account.nickname}}：</span>
        {{parentData.content}}
      </div>
      <el-row>
        <el-col v-for="item in parentData.pics" :key="item.id" :span="3" class="pic">
          <img @click="showImg" :src="$axios.defaults.baseURL + item.url" alt />
        </el-col>
      </el-row>
      <div class="func">
        <div class="time">{{time}}</div>
        <a href="javascript:;" class="reply" @click="reply">回复</a>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" size="tiny">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
    <Parent :parentData="parentData.parent" v-if="parentData.parent" @parentCallReply="callReply" />
  </div>
</template>

<script>
import moment from "moment";
export default {
  props: ["parentData"],
  name: "Parent",
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  computed: {
    time() {
      return moment(this.parentData.created_at).format("YYYY-MM-DD HH:mm");
    },
  },
  methods: {
    showImg(e) {
      this.dialogImageUrl = e.path[0].src;
      this.dialogVisible = true;
    },
    //点击回复跳到文本框
    returnTop() {
      document.querySelector("#dingbu").scrollIntoView(true);
    },
    //触发外层事件，传递参数
    reply() {
      this.returnTop();
      this.$emit("parentCallReply", {
        id: this.parentData.id,
        nickname: this.parentData.account.nickname,
      });
    },
    // 递归调用，接收参数
    callReply(obj) {
      this.$emit("parentCallReply", obj);
    },
  },
};
</script>

<style lang="less" scoped>
.commentReply {
  > .cmtBody {
    padding: 5px 10px;
  }
  background: #eaeaec;
  .userSay {
    span {
      color: #ffa500;
    }
  }
  .pic {
    margin: 5px;
    img {
      width: 100%;
      height: 100%;
      display: block;
      object-fit: cover;
      cursor: pointer;
    }
  }
  .func {
    margin: 5px 0;
    display: flex;
    justify-content: space-between;
    a {
      &:hover {
        text-decoration: underline;
        color: #ffa500;
      }
    }
  }
}
</style>